package com.lkwlw.tplms.action;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.lkwlw.tplms.model.DriverInfo;
import com.lkwlw.tplms.model.DriverInfoExample.Criteria;
import com.lkwlw.tplms.mapper.DriverInfoMapper;
import com.lkwlw.tplms.model.DriverInfoExample;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DriverAction extends ActionSupport{

	private static final long serialVersionUID = 3226777258294107128L;
	private List<DriverInfo> driverList;
	private String selectIds;
	private DriverInfoMapper driverDAO;
	private DriverInfo driver;
	private String birthday;	
	private String validFrom;
	public String execute() throws Exception{
		DriverInfoExample example = new DriverInfoExample();
		if(driver != null){
			Criteria criteria = example.createCriteria();
			if (null != driver.getDriver() && !"".equals(driver.getDriver())) {
				criteria.andDriverLike("%" + driver.getDriver() + "%");
			}
			if (null != driver.getIdCardNo() && !"".equals(driver.getIdCardNo())) {
				criteria.andIdCardNoLike("%" + driver.getIdCardNo() + "%");
			}
			if (null != driver.getState() && !"".equals(driver.getState())) {
				criteria.andStateEqualTo(driver.getState());
			}
		}
		driverList = driverDAO.selectByExample(example);
		return SUCCESS;
	}
	public String add() throws Exception{
		selectIds = "";
		birthday = "";
		validFrom = "";
		return SUCCESS;
	}
	public String edit() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String idCardNo = request.getParameter("editDriverId");
		driver = driverDAO.selectByPrimaryKey(idCardNo);
		// 把性别、驾照类型和状态的值拼凑成字符串，供修改时使用
		selectIds = String.valueOf(driver.getSex());
		selectIds = selectIds + "," + driver.getLicenseType() + "," + driver.getState();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		birthday = format.format(driver.getBirthday());
		validFrom = format.format(driver.getValidFrom());
		return SUCCESS;
	}
	public String driverInfoDetail() throws Exception {
		
		return SUCCESS;
	}
	public String delete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("deleteDriverIds");
		String[] deleteIds = ids.split(",");
		driverDAO.deleteDriverInfo(deleteIds);
		execute();
		return SUCCESS;
	}
	public String save() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		Admininfo admin = (Admininfo) session.get("admin");
		driver.setRegistrant(admin.getFullName());
		HttpServletRequest request = ServletActionContext.getRequest();
		String birthday1 = request.getParameter("birthday1");
		String validFrom1 = request.getParameter("validFrom1");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date birthday2 = format.parse(birthday1);
		Date validFrom2 = format.parse(validFrom1);
		driver.setBirthday(birthday2);
		driver.setValidFrom(validFrom2);
		if (selectIds.equals("")) {
			driverDAO.insertSelective(driver);
		}else {
			driverDAO.updateByPrimaryKeySelective(driver);
		}
		driver = null;
		//driverDAO.findDriverByCondition(driver);
		execute();
		return SUCCESS;
	}
	public DriverInfo getDriver() {
		return driver;
	}
	public void setDriver(DriverInfo driver) {
		this.driver = driver;
	}
	public DriverInfoMapper getDriverDAO() {
		return driverDAO;
	}
	public void setDriverDAO(DriverInfoMapper driverDAO) {
		this.driverDAO = driverDAO;
	}
	public List<DriverInfo> getDriverList() {
		return driverList;
	}
	public void setDriverList(List<DriverInfo> driverList) {
		this.driverList = driverList;
	}
	public String getSelectIds() {
		return selectIds;
	}
	public void setSelectIds(String selectIds) {
		this.selectIds = selectIds;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getValidFrom() {
		return validFrom;
	}
	public void setValidFrom(String validFrom) {
		this.validFrom = validFrom;
	}
}
